/*
 * Story1Layer.cpp
 *
 *  Created on: 2015-9-21
 *      Author: gaoyu
 */

#include "Story1Layer.h"
#include "ActionShowWidget.h"
#include "MapScreen.h"
#include "LocalizedStringGetter.h"
#include "StoryConfig.h"

#define REST_TIME   1

typedef enum {
    ZBg,
    ZAction,
    ZBoss,
    ZMonster,
    ZRoler,



} ZIndex;

Story1Layer::Story1Layer()
:m_map(NULL)
,princess(NULL)
,m_objectMap(NULL)
,m_state(0)
,m_lastTime(0)
,m_isOpen(false)
{
}

Story1Layer::~Story1Layer() {

}

Scene *Story1Layer::createScene()
{
    auto scene = Scene::create();
    auto layer = Story1Layer::create();
    scene->addChild(layer);
    return scene;
}

bool Story1Layer::init()
{
    if (!Layer::init()) {
        return false;
    }

    mapInit();
    roleInit();
    scheduleUpdate();
    return true;
}

void Story1Layer::mapInit()
{
	m_map = TMXTiledMap::create("map01.tmx");
	addChild(m_map);
	m_objectMap = m_map->getObjectGroup("ObjectLayer");
}

void Story1Layer::roleInit()
{
	princess = Roler::create(RolerTypePrincess);
	princess->setPosition(getPointFromObjectLayer("PrincessStartPoint"));
	princess->setCallback(CC_CALLBACK_1(Story1Layer::princessMoveEndCallback, this));
	addChild(princess,ZRoler);


	prince = Roler::create(RolerTypePrince);
	prince->setPosition(getPointFromObjectLayer("PrinceStartPoint"));
	prince->setCallback(CC_CALLBACK_1(Story1Layer::princeMoveEndCallback, this));
	addChild(prince,ZRoler);

    boss1 = Roler::create(RolerTypeBoss1);
    boss1->setPosition(getPointFromObjectLayer("Boss1StartPoint"));
    boss1->setCallback(CC_CALLBACK_1(Story1Layer::boss1MoveEndCallback, this));
    addChild(boss1,ZBoss);
    boss1->setVisible(false);

    monster1 = Roler::create(RolerTypeMonster1);
    monster1->setPosition(getPointFromObjectLayer("Monster1StartPoint"));
    monster1->setCallback(CC_CALLBACK_1(Story1Layer::monster1MoveEndCallback, this));
    addChild(monster1,ZMonster);
    monster1->setVisible(false);

    monster2 = Roler::create(RolerTypeMonster2);
    monster2->setPosition(getPointFromObjectLayer("Monster2StartPoint"));
    monster2->setCallback(CC_CALLBACK_1(Story1Layer::monster2MoveEndCallback, this));
    addChild(monster2,ZMonster);
    monster2->setVisible(false);

    stateChangePrepare();
}

Point Story1Layer::getPointFromObjectLayer(char* name)
{
	ValueMap value = m_objectMap->objectNamed(name);
	return Point(value["x"].asInt(),
			value["y"].asInt());
}

void Story1Layer::princessMoveEndCallback(Ref *sender)
{
	if(m_state == 2||m_state == 3||m_state == 5){
		stateChangePrepare();
	}

}

void Story1Layer::princeMoveEndCallback(Ref *sender)
{
	if(m_state == 1){
		stateChangePrepare();
	}
}

void Story1Layer::boss1MoveEndCallback(Ref *sender)
{
	if(m_state == 6){
		stateChangePrepare();
	}
    if((m_state == 8)&&(!boss1->getIsTalk())){
    	StoryConfig::getInstance()->storyShowedSave(StoryConfigType1);
    	Director::getInstance()->replaceScene( TransitionProgressOutIn::create(2,MapScreen::createScene()));
    }

}

void Story1Layer::monster1MoveEndCallback(Ref *sender)
{
	if(m_state == 7){
		monster1->setDirection(RolerDirectDown);
		stateChangePrepare();
	}
}

void Story1Layer::monster2MoveEndCallback(Ref *sender)
{
	monster2->setDirection(RolerDirectDown);
}

void Story1Layer::actionEndCallback(Ref *sender)
{
	if(m_state == 4){
		stateChange();
	}
}

void Story1Layer::stateChange()
{
    switch(m_state){
        case 0:
        	prince->talk(LocalizedStringGetter::getStringByKey("StoryTalk1_1"));
        	prince->setDirection(RolerDirectLeft);
    	    break;

        case 1:
        	princess->talk(LocalizedStringGetter::getStringByKey("StoryTalk1_2"));
        	princess->setDirection(RolerDirectRight);
    	    break;

        case 2:
        	princess->move(RolerDirectUp,10);
        	prince->move(RolerDirectUp,10);
        	break;

        case 3:
        	createAction();
        	break;

        case 4:
        	boss1->setVisible(true);
        	princess->showAction(RolerActionTypeQuery);
        	prince->showAction(RolerActionTypeQuery);
        	break;

        case 5:
        	boss1->move(RolerDirectDown,4);
        	break;

        case 6:
        	boss1->move(RolerDirectUp,5);
        	prince->setDirection(RolerDirectDown);
        	prince->moveAndNoDirectionChange(RolerDirectUp,5);
        	prince->talk(LocalizedStringGetter::getStringByKey("StoryTalk1_3"));
        	monster1->setVisible(true);
        	monster1->move(RolerDirectRight,8);
        	monster2->setVisible(true);
        	monster2->move(RolerDirectLeft,10);
        	break;
        case 7:
        	boss1->move(RolerDirectUp,20);
        	boss1->talk(LocalizedStringGetter::getStringByKey("StoryTalk1_4"));
    	    prince->setDirection(RolerDirectDown);
    	    prince->moveAndNoDirectionChange(RolerDirectUp,20);
        	princess->move(RolerDirectUp,2);
        	princess->talk(LocalizedStringGetter::getStringByKey("StoryTalk1_5"));
        	break;
    }
    m_state++;
}

void Story1Layer::update(float dt)
{
	if(!m_isOpen) return;

    if(m_lastTime < REST_TIME){
    	m_lastTime += dt;
    }else{
    	m_isOpen = false;
    	stateChange();
    }
}

void Story1Layer::stateChangePrepare()
{
	m_lastTime = 0;
	m_isOpen = true;
}

void Story1Layer::createAction()
{
	ActionShowWidget *widget = ActionShowWidget::create(ActionShowWidgetTypeTransferBlack);
	widget->setPosition(getPointFromObjectLayer("Boss1StartPoint"));
	widget->setCallback(CC_CALLBACK_1(Story1Layer::actionEndCallback, this));
	addChild(widget,ZAction);
}
